package com.example.hrandroidapp;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.hrandroidapp.adapter.ServerListViewAdapter;
import com.example.hrandroidapp.adapter.ServerListViewAdapter.EditServerInterface;
import com.example.hrandroidapp.common.Constant;
import com.example.hrandroidapp.common.HRLog;
import com.example.hrandroidapp.domain.ServerPO;
import com.example.hrandroidapp.preferences.ServerListPreference;
import com.gc.materialdesign.views.ButtonFlat;
import com.gc.materialdesign.views.ButtonFloat;
import com.gc.materialdesign.views.CheckBox;

public class AdvanceSettingActivity extends ActionBarActivity implements OnItemClickListener, EditServerInterface {

	private ImageView btnClose;
	private ListView lstServers;
	private ServerListViewAdapter svrLstAdapter;
	private List<ServerPO> serverList;
	private ButtonFloat btnAddServer;
	
	private static RelativeLayout rlAddSvrDialog;
	private static TextView addSvrTitle;
	private static EditText newSvrName, ipAddress;
	private static ButtonFlat btnFlatDone, btnFlatCancel;
	
	private ServerListPreference svrLstPref;
	
	private static TranslateAnimation anim;
	private Resources res;
	private static String edit, delete, add, cancel;
	
	private int editSvrPos=-1;

	@Override
	protected void onPause() {
		super.onPause();
		svrLstPref.saveServers(this, svrLstAdapter.getServerList());
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
		setContentView(R.layout.advance_setting);
		
		ActionBar actionBar = getSupportActionBar();
		actionBar.hide();
		
		loadViews();
		res = getResources();
		
		addSvrTitle.setTypeface(null, Typeface.BOLD);
		edit = res.getString(R.string.btn_edit);
		delete = res.getString(R.string.btn_delete);
		add = res.getString(R.string.btn_add);
		cancel = res.getString(R.string.btn_cancel);
		
		btnClose.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
				overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
			}
		});
		
		btnAddServer.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				HRLog.postLogInfo("click on add server button", "AdvancedSettingActivity.onCreate");
				showAddServerDialog(null);
			}
		});
		
		rlAddSvrDialog.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss(v);
			}
		});
		
		btnFlatDone.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss(v);
			}
		});
		
		btnFlatCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss(v);
			}
		});
		
		loadServers();
		loadAdapter(); 
		
		lstServers.setOnItemClickListener(this);
	}
	
	public static void showAddServerDialog(ServerPO svrPO) {

		if(svrPO != null) {
			addSvrTitle.setText("Edit Server");
			newSvrName.setText(svrPO.getSvrName());
			ipAddress.setText(svrPO.getSvrIP());
			
			btnFlatDone.setText(edit);
			btnFlatCancel.setText(delete); 
		}else {
			addSvrTitle.setText("Add New Server");
			newSvrName.setText("");
			ipAddress.setText("");
			
			btnFlatDone.setText(add);
			btnFlatCancel.setText(cancel); 
		}
		
		int translationY = 0;
		rlAddSvrDialog.setVisibility(View.VISIBLE);
		if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB) {
			anim = new TranslateAnimation(0, 0, translationY, translationY);
			anim.setFillAfter(true);
			anim.setDuration(0);
			rlAddSvrDialog.startAnimation(anim);
		} else {
			rlAddSvrDialog.setTranslationY(translationY);
		}
	}
	
	private void dismiss(View v) {
		switch (v.getId()) {
		case R.id.btn_save: 
			HRLog.postLogInfo("New Server Name/IP: " + newSvrName.getText().toString() + "/" + ipAddress.getText().toString(), 
					"AdvanceSetting.dismiss"); 
			
			if(((ButtonFlat)v).getText().equalsIgnoreCase(res.getString(R.string.btn_edit))) {
				serverList.get(editSvrPos).editServer(newSvrName.getText().toString(), ipAddress.getText().toString()); 
			}else if(((ButtonFlat)v).getText().toString().equalsIgnoreCase(res.getString(R.string.btn_add))) {
				boolean flag = (Constant.CURRENT_IP == null)? true : false;
				ServerPO svrPO = new ServerPO(newSvrName.getText().toString(), ipAddress.getText().toString(), flag);
				serverList.add(svrPO);
			}
			loadAdapter();
			
			break;
			
		case R.id.btn_cancel:
			if(((ButtonFlat)v).getText().equalsIgnoreCase(res.getString(R.string.btn_delete))) {
				serverList.remove(editSvrPos);
				loadAdapter();
			}
			break;
			
		}
		rlAddSvrDialog.setVisibility(View.GONE);
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(ipAddress.getWindowToken(), 0);
	}

	private void loadAdapter() {
		svrLstAdapter = new ServerListViewAdapter(this, serverList, this);
		lstServers.setAdapter(svrLstAdapter);
		svrLstPref.saveServers(getApplicationContext(), serverList);
	} 

	private void loadServers() {
		
		serverList = svrLstPref.getServers(getApplicationContext());
		
		if(serverList == null)
			serverList = new ArrayList<ServerPO>();

	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		boolean flag = ((CheckBox) parent.findViewById(R.id.chk_server)).isCheck();
		((CheckBox) parent.findViewById(R.id.chk_server)).setChecked(!flag);
		
		svrLstAdapter.setServers(serverList, position); 
	}
	
	private void loadViews() {
		svrLstPref = new ServerListPreference();
		btnAddServer = (ButtonFloat) findViewById(R.id.btn_add_server);
		btnClose = (ImageView) findViewById(R.id.img_close);
		lstServers = (ListView) findViewById(R.id.lv_servers);
		serverList = new ArrayList<ServerPO>();
		
		rlAddSvrDialog = (RelativeLayout) findViewById(R.id.add_svr_dialog);
		addSvrTitle = (TextView) findViewById(R.id.tv_addsvr_title);
		newSvrName = (EditText) findViewById(R.id.et_svr_name); 
		ipAddress = (EditText) findViewById(R.id.et_ip_addr);
		btnFlatDone = (ButtonFlat) findViewById(R.id.btn_save); 
		btnFlatCancel = (ButtonFlat) findViewById(R.id.btn_cancel);
	}

	@Override
	public void editServerCallback(ServerPO svrPO, int position) {
		editSvrPos = position;
		showAddServerDialog(svrPO); 
	}

}
